package com.wenhuitec.android.tang.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.dhkj.web.bean.ProductModel;
import com.wenhuitec.android.tang.R;

import java.util.ArrayList;
import java.util.List;

public class ShaiXuanAdapter extends RecyclerView.Adapter<ShaiXuanAdapter.MyViewHolder> {

    private LayoutInflater inflater;

    private Context mContext;

    private int select = -1;

    List<ProductModel> datas = new ArrayList<>();

    private UserClick userClick;

    public interface UserClick {
        void onUserClick(ProductModel productModel);
    }

    public ShaiXuanAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        mContext = context;
    }

    public void setUserClick(UserClick userClick) {
        this.userClick = userClick;
    }

    public void setDatas(List<ProductModel> datas) {
        this.datas = datas;
        select = -1;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_str2, parent, false);
        return new MyViewHolder(itemView);
    }


    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.nameTv.setText(datas.get(position).getClsname());
        if (select == position) {
            GradientDrawable drawable = new GradientDrawable();
            drawable.setShape(GradientDrawable.RECTANGLE);
            drawable.setColor(Color.argb(127, 215, 10, 10));
            drawable.setCornerRadius(18);
            holder.nameTv.setPadding(15, 15, 15, 15);
            holder.nameTv.setGravity(Gravity.CENTER);
            holder.nameTv.setBackground(drawable);

        } else {
            GradientDrawable drawable = new GradientDrawable();
            drawable.setShape(GradientDrawable.RECTANGLE);
            drawable.setColor(Color.argb(127, 215, 229, 235));
            drawable.setCornerRadius(18);
            holder.nameTv.setPadding(15, 15, 15, 15);
            holder.nameTv.setGravity(Gravity.CENTER);
            holder.nameTv.setBackground(drawable);
        }

        holder.nameTv.setOnClickListener(view -> {
            if (select == position) {
                select = -1;
                if (userClick != null) {
                    userClick.onUserClick(null);
                }
            } else {
                select = position;
                if (userClick != null) {
                    userClick.onUserClick(datas.get(position));
                }
            }

            notifyDataSetChanged();

        });
    }

    //数据源的数量
    @Override
    public int getItemCount() {
        return datas.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView nameTv;


        public MyViewHolder(View itemView) {
            super(itemView);
            nameTv = (TextView) itemView.findViewById(R.id.nameTv);

        }
    }
}
